package com.yc.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.yc.bean.Cartinfo;
import com.yc.biz.CartinfoBiz;
import com.yc.biz.impl.CartinfoBizImpl;

@WebServlet("/cartinfoServlet")
public class CartinfoServlet extends HttpServlet{
	CartinfoBiz cb=new CartinfoBizImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		PrintWriter out=resp.getWriter();
		String op=req.getParameter("op");
		
		if("addCart".equals(op)){
			addCart(req,resp,out);
		}else if("findCart".equals(op)){
			findCart(req,resp,out);
		}else if("updateNum".equals(op)){
			updateNum(req,resp,out);
		}else if("del".equals(op)){
			del(req,resp,out);
		}else if("countAll".equals(op)){
			countAll(req,resp,out);
		}else if("showorder".equals(op)){
			showorder(req,resp,out);
		}
	}

	//订单页面的显示
	private void showorder(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		String str=req.getParameter("str");
		int uid=Integer.parseInt(req.getSession().getAttribute("uid")+"");
		
		List<Map<String, String>> list=cb.show(uid,str);
		if(list.isEmpty()){
			out.print(0);
			return;
		}
		Gson json=new Gson();
		out.print(json.toJson(list));
	}

	//计算所有
	private void countAll(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int uid=Integer.parseInt(req.getSession().getAttribute("uid")+"");
		List<Map<String, String>> list=cb.countAll(uid);
		if(list.size()>0){
			Gson gson=new Gson();
			out.print(gson.toJson(list));
		}else{
			out.print(0);
		}
		
	}

	//删除购物车的某件商品
	private void del(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int uid=Integer.parseInt(req.getSession().getAttribute("uid")+"");
		int gid=Integer.parseInt(req.getParameter("gid"));
		
		if(cb.del(gid,uid)>0){
			out.print(1);
			//说明删除成功  应该在重新获取一下购物车的信息然后存session
			req.getSession().setAttribute("cartinfo", cb.findAll(uid));
		}else{
			out.print(0);
		}
	}

	//更新购物车的某个商品的数量
	private void updateNum(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int uid=Integer.parseInt(req.getSession().getAttribute("uid")+"");
		int gid=Integer.parseInt(req.getParameter("gid"));
		int num=Integer.parseInt(req.getParameter("num"));
		
		int result=cb.updateNum(uid, gid, num);
		if(result>0){
			out.print(1);
			//说明修改成功  应该在重新获取一下购物车的信息然后存session
			req.getSession().setAttribute("cartinfo", cb.findAll(uid));
		}else{
			out.print(0);
		}
	}

	//查询购物的信息
	private void findCart(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int uid=Integer.parseInt(req.getSession().getAttribute("uid")+"");
		
		List<Cartinfo> cartinfo=cb.findAll(uid);
		
		if(cartinfo.isEmpty()){
			out.print(0);
			return;
		}
		req.getSession().setAttribute("cartinfo", cartinfo);
		out.print(1);
	}

	//添加商品到购物车
	private void addCart(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int gid=Integer.parseInt(req.getParameter("gid"));
		int uid=Integer.parseInt(req.getSession().getAttribute("uid")+"");

		
		int result=cb.addCart(uid,gid);
		if(result>0){
			//添加成功后应该查询一下购物车的数量并且存到session中
			req.getSession().setAttribute("cartNum", cb.findCartNum(uid));
			out.print(1);
		}else{
			out.print(0);
		}
		
		
	}

}
